package com.mdd.common.util.aliyunOperatorLogin;

import com.aliyun.dypnsapi20170525.models.GetMobileResponse;
import com.aliyun.dypnsapi20170525.models.GetMobileResponseBody;
import com.aliyun.tea.TeaException;
import org.apache.commons.math3.util.Pair;


public class GetMobile {

//    public static void main(String[] args) {
//        String regionId = "cn-hangzhou"; // 区域ID
//        String accessKey = "your-access-key-id"; // AccessKey ID
//        String accessSecret = "your-access-key-secret"; // AccessKey Secret
//        String spToken = "client-sp-token"; // 从客户端获取的spToken
//
//        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKey, accessSecret);
//        IAcsClient client = new DefaultAcsClient(profile);
//
//        GetMobileRequest request = new GetMobileRequest();
//        request.setAccessToken(spToken); // 设置客户端传入的spToken
//
//        try {
//            GetMobileResponse response = client.getAcsResponse(request);
//            if ("OK".equals(response.getBody())) {
//                System.out.println("手机号：" + response.getGetMobileResultDTO().getMobile());
//            } else {
//                System.err.println("错误码：" + response.getCode() + "，错误信息：" + response.getMessage());
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * <b>description</b> :
     * <p>使用AK&amp;SK初始化账号Client</p>
     * @return Client
     *
     * @throws Exception
     */
    public static com.aliyun.dypnsapi20170525.Client createClient() throws Exception {
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html。
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId("LTAI5t6J3udSCwjEh8kPrSTd")
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret("5gbOEgbhHzNyrcW0meMACetgn9kY4X");
        // Endpoint 请参考 https://api.aliyun.com/product/Dypnsapi
//        config.endpoint = "dypnsapi.aliyuncs.com";
        config.endpoint = "dypnsapi.aliyuncs.com";
        return new com.aliyun.dypnsapi20170525.Client(config);
    }

    /**
     * 阿里云手机号
     * @param accessToken
     * @return
     * @throws Exception
     */
    public static Pair<Boolean, GetMobileResponseBody.GetMobileResponseBodyGetMobileResultDTO> getMobile(String accessToken) throws Exception {
        com.aliyun.dypnsapi20170525.Client client = createClient();
        com.aliyun.dypnsapi20170525.models.GetMobileRequest getMobileRequest = new com.aliyun.dypnsapi20170525.models.GetMobileRequest()
                .setAccessToken(accessToken);
//                .setOutId("22345****");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            GetMobileResponse response = client.getMobileWithOptions(getMobileRequest, runtime);
            if(response!=null && response.getBody()!=null){
                GetMobileResponseBody body = response.getBody();
                String code = body.getCode();
                GetMobileResponseBody.GetMobileResponseBodyGetMobileResultDTO resultDTO =body.getGetMobileResultDTO();
                String mobile = resultDTO.getMobile();
                String requestId = body.getRequestId();
                if("OK".equals(code)){
                    return new Pair<>(true,resultDTO);
                }
            }
//            System.out.println(JSONObject.toJSON(response));
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return new Pair<>(false,null);
    }


    public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.dypnsapi20170525.Client client = createClient();
        com.aliyun.dypnsapi20170525.models.GetMobileRequest getMobileRequest = new com.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SjdRR2FtWUppTGJMNmcrbmI0NTZDNVhkcVM4MlNLNnd2Q0hEXC84bzFkRXg1S0tISmZvSUtVcXp6cVBvSlNqTHMzbVVJbjUwcTFPTlFIdm5cLzdOZmNPdGswZ2NWVTl6M3BadlBNZVV3RUhcL0R6MW5lUFpjQWYxdXBQRWVrcG95QW1aVGUrZGE0RytodmY1c1wvbXdDNXBUS0trRlBCaDNzaHViRmlSZW1hK0RjNjdUU29qbWMxMGFJMXBXN2grYzZTM1RscFZPY0ZMSEtFRHA2RTJsSkVoRHl1N3g2OTRPYWVzUXNqVTYwOG5QQXJGTWs5T0lqUjg4UEx0TzJBOG1NMlh0ZVNIRFptSEhtMUhjcjJNZEloXC9scUMwS0F3K1wvMlh2V05ZZ1pEUHF2dmx0YUNseGhuU3pMeTg1TTlzS2RzeGdrZnk4QTF0eFRXNVwvTGJ6MGp5c2R4TlR2dFRcL3lzQXliTVJnNXM3ZDJZdndqZmlDWkwrUGl0c0tPYXEwcjlxVFhuVlJWaGNrQ01hTm80OUFMQ2hFQm9WUT09In0=");
//                .setOutId("22345****");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            GetMobileResponse response = client.getMobileWithOptions(getMobileRequest, runtime);
            GetMobileResponseBody body = response.getBody();
            //OK
            String code = body.getCode();
            GetMobileResponseBody.GetMobileResponseBodyGetMobileResultDTO resultDTO =body.getGetMobileResultDTO();
            String mobile = resultDTO.getMobile();
            String requestId = body.getRequestId();

            System.out.println(response);


        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }


}
